package com.netease.nie.base.mybatis.sql;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.netease.nie.base.utils.ConvertUtils;

/**
 * 
 * @author KingsHunter
 * @createDate July 12th,2016
 *
 */
public class Shard {
	
	private List<SqlParam> sqlParams = new ArrayList<SqlParam>();
    private List<Object> values = new ArrayList<Object>();
    private String fname;
    private boolean isMultiShard;
    
    public String getFname() {
        return this.fname;
    }

    /** sql params */
    public List<SqlParam> getSqlParams() {
        List<SqlParam> results = new ArrayList<SqlParam>();
        for (SqlParam sqlParam : sqlParams) {
            results.add(sqlParam);
        }
        return results;
    }

    /** get multiply table value */
    public List<Object> getValues() {
        return this.values;
    }

    public boolean isMulti() {
        return this.isMultiShard;
    }

    public <T> Shard(String fname, T ... values) throws Exception {
        List<Object> params = ConvertUtils.getObjectList(values);
        shardParse(fname, params);
    }
    
    public <T> Shard() throws Exception {
    	
    }

    private void shardParse(String fname, List<Object> values) throws Exception {
         if (StringUtils.isBlank(fname) || values == null || values.isEmpty()) {
             throw new Exception();
         }

         this.fname = fname.trim();
         this.isMultiShard = values.size() > 1 ? true : false;
         for (Object value : values) {
             SqlParam sqlParam = new SqlParam(this.fname, value);
             this.sqlParams.add(sqlParam);
             this.values.add(value);
         }

         String[] entity_field = fname.split("\\.");
         if (entity_field.length != 2) {
        	 throw new Exception();
         }

         String className = entity_field[0];
         String fieldName = entity_field[1];

         // 校验实体类
         

         // 校验实体类是否存在字段
         

         this.sqlParams.clear();
         
         return;
    }
}
